package io.gitee.bluemiaomiao.dataanalyzebackend.controller;

import io.gitee.bluemiaomiao.dataanalyzebackend.domian.LoginDto;
import io.gitee.bluemiaomiao.dataanalyzebackend.domian.Ruser;
import io.gitee.bluemiaomiao.dataanalyzebackend.domian.RuserInfo;
import io.gitee.bluemiaomiao.dataanalyzebackend.service.RoleService;
import io.gitee.bluemiaomiao.dataanalyzebackend.service.RuserService;
import io.gitee.bluemiaomiao.dataanalyzebackend.util.ResultCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: TLang @Date: 2020/11/6
 *
 * @description:
 */
@RestController
@RequestMapping(value = "/User")
public class LoginController {

  @Autowired RuserService ruserService;
  @Autowired RoleService roleService;

  /** 登录接口 */
  @PostMapping(value = "/logintest")
  public ResultCodeUtil login(@RequestBody LoginDto loginBean, HttpServletRequest request)
      throws IOException {
    String username = loginBean.getUsername();
    String password = loginBean.getPassword();

    return ResultCodeUtil.success(null, "success");
  }

  @PostMapping(value = "/add")
  public ResultCodeUtil AddUser(Ruser ruser) {
    return ResultCodeUtil.success(ruserService.insert(ruser), "success");
  }

  @PostMapping(value = "/GetAllRole")
  public ResultCodeUtil GetAllRole() {
    return ResultCodeUtil.success(roleService.queryAll(), "success");
  }

  @PostMapping(value = "/GetAllUser")
  public ResultCodeUtil GetAllUser() {
    return ResultCodeUtil.success(ruserService.queryAll(), "success");
  }

  @PostMapping(value = "put")
  public ResultCodeUtil putUser(RuserInfo ruserInfo) {
    ruserService.update(ruserInfo);
    return ResultCodeUtil.success(null, "修改成功");
  }

  @PostMapping(value = "delete")
  public ResultCodeUtil DeleteUser(int id) {
    ruserService.deleteById(id);
    return ResultCodeUtil.success(null, "删除成功");
  }

  @GetMapping(value = "/GetUserInfo")
  public ResultCodeUtil GetUserInfo() {
    Ruser ruser = (Ruser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    System.out.println(ruser.getUsername());
    return ResultCodeUtil.success(ruser, "success");
  }
}
